package com.gpframework.module.purchase.service.impl;

import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.purchase.domain.*;
import com.gpframework.module.purchase.domain.req.GpPurchaseQualityFeedbackUpdate;
import com.gpframework.module.purchase.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.purchase.mapper.GpPurchaseQualityFlowDetailsMapper;
import com.gpframework.module.purchase.domain.vo.GpPurchaseQualityFlowDetailsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpPurchaseQualityFlowDetails
 * @Author gpframework
 * @Date 2024-01-09
 * @Version 1.0.0
 * @Description 采购品质问题跟踪明细Service业务层处理
 */
@Service
public class GpPurchaseQualityFlowDetailsServiceImpl extends ServiceImpl<GpPurchaseQualityFlowDetailsMapper, GpPurchaseQualityFlowDetails> implements IGpPurchaseQualityFlowDetailsService {

    @Autowired
    private IGpPurchaseQualityFeedbackService iGpPurchaseQualityFeedbackService;
    @Autowired
    private IGpPurchaseIncomingInspectService iGpPurchaseIncomingInspectService;
    @Autowired
    private IGpPurchaseReceiptService iGpPurchaseReceiptService;
    @Autowired
    private IGpPurchasingMakingDetailsService iGpPurchasingMakingDetailsService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [lqw]
     * @Return List<GpPurchaseQualityFlowDetailsVo>
     */
    @Override
    public List<GpPurchaseQualityFlowDetailsVo> listVo(QueryWrapper<GpPurchaseQualityFlowDetails> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [id]
     * @Return GpPurchaseQualityFlowDetailsVo
     */
    @Override
    public GpPurchaseQualityFlowDetailsVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public boolean qualityIssueTrackDetails(GpPurchaseQualityFeedbackUpdate gpPurchaseQualityFeedbackUpdate) {
        List<Long> ids = gpPurchaseQualityFeedbackUpdate.getIds();
        GpPurchaseQualityFeedback gpPurchaseQualityFeedback;
        List<GpPurchaseQualityFlowDetails> list = new ArrayList<>();
        for (Long id : ids) {
            gpPurchaseQualityFeedback = iGpPurchaseQualityFeedbackService.getById(id);
            if (gpPurchaseQualityFeedback == null) {
                continue;
            }
            GpPurchaseQualityFlowDetails gpPurchaseQualityFlowDetails = new GpPurchaseQualityFlowDetails();
            BeanUtils.copyProperties(gpPurchaseQualityFeedback,gpPurchaseQualityFlowDetails);
            if (StringUtils.isNotBlank(gpPurchaseQualityFeedbackUpdate.getCreateBy())) {
                gpPurchaseQualityFlowDetails.setCreateBy(gpPurchaseQualityFeedbackUpdate.getCreateBy());
            }
            gpPurchaseQualityFlowDetails.setQualityFeedbackId(gpPurchaseQualityFeedback.getId());
            gpPurchaseQualityFlowDetails.setProblemDescription(gpPurchaseQualityFeedback.getFeedbackContent());
            gpPurchaseQualityFlowDetails.setProcessingScheme(gpPurchaseQualityFeedback.getReplyContent());
            // 从来料检验中获取数量（空码）
            GpPurchaseIncomingInspect gpPurchaseIncomingInspect = iGpPurchaseIncomingInspectService.getById(gpPurchaseQualityFeedback.getIncomingInspectId());
            if (gpPurchaseIncomingInspect != null) {
                gpPurchaseQualityFlowDetails.setEmptyQuantity(gpPurchaseIncomingInspect.getTotalQuantity());
                // 从发货单中获取发货日期
                GpPurchaseReceipt gpPurchaseReceipt = iGpPurchaseReceiptService.getById(gpPurchaseIncomingInspect.getPurchaseReceiptId());
                if (gpPurchaseReceipt != null) {
                    gpPurchaseQualityFlowDetails.setArrivalTime(gpPurchaseReceipt.getSendTime());
                }
                // 从排单表中获取供应商
                GpPurchasingMakingDetails gpPurchasingMakingDetails = iGpPurchasingMakingDetailsService.getById(gpPurchaseIncomingInspect.getPurchasingDetailsId());
                if (gpPurchasingMakingDetails != null) {
                    gpPurchaseQualityFlowDetails.setSupplierName(gpPurchasingMakingDetails.getSupplier());
                }
            }
            list.add(gpPurchaseQualityFlowDetails);

        }
        return this.saveBatch(list);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpPurchaseQualityFlowDetails gpPurchaseQualityFlowDetails = this.getById(id);
        if (gpPurchaseQualityFlowDetails == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpPurchaseQualityFlowDetails.setId(null);
            this.save(gpPurchaseQualityFlowDetails);

        }
        return true;
    }
}
